using System;

namespace ClownFish
{
	public abstract class DbContextHolderBase : IDbContextHolder
	{
		private DbContext dbContext_0;

		private bool bool_0;

		public DbContext DbContext
		{
			get
			{
				if (this.dbContext_0 == null)
				{
					this.dbContext_0 = this.CreateDbContext(false);
					this.bool_0 = true;
				}
				return this.dbContext_0;
			}
			set
			{
				this.Dispose();
				this.dbContext_0 = value;
			}
		}

		public void Dispose()
		{
			if (this.dbContext_0 != null && this.bool_0)
			{
				this.dbContext_0.Dispose();
				this.dbContext_0 = null;
				this.bool_0 = false;
			}
		}

		public virtual DbContext CreateDbContext(bool supportTranscation)
		{
			return new DbContext(supportTranscation);
		}

		public T CreateHolder<T>() where T : class, IDbContextHolder, new()
		{
			T result = Activator.CreateInstance(typeof(T)) as T;
			result.DbContext = this.DbContext;
			return result;
		}
	}
}
